<!--
 * @Description: 教师业绩模块-专著与主编教材
 * @Version: 1.0
 * @Autor: tao
 * @Date: 2021-01-15 16:19:27
 * @LastEditors: tao
 * @LastEditTime: 2021-04-25 13:19:55
-->

<template>
  <div class="Inventory">
    <Management>
      <Top
        slot="top"
        @showModel="showModel"
        @showAll="showAll"
        @search="onSearch"
        :adding="true"
        :excel="true"
        @MakeExcel="ToExcel"
      ></Top>
      <Table
        slot="body"
        :title="columns"
        :tableData="tableData"
        :total="total"
        :current="current"
        @edit="edit"
        @del="del"
        @changePage="changePage"
      ></Table>
    </Management>
    <Dialog
      v-if="isShow"
      :show="isShow"
      :row="row"
      @confirm="confirm"
      @cancel="cancel"
    >
    </Dialog>
  </div>
</template>

<script>
//组件
import Table from "components/common/table/Table";
import Management from "components/common/management/Management";
import Top from "./component/Top";
import Dialog from "./component/Dialog";
//网络请求
import {
  getDataByPageAndSize,
  appendData,
  deleteById,
  editData,
  LikeSearch,
  Excel
} from "@/network/match/match";
import { getSystem } from "@/network/Info/BaseInfo";
import {downloadOfA} from "@/common";

export default {
  //注册组件
  components: {
    Table,
    Top,
    Management,
    Dialog,
  },
  data() {
    const columns = [
      {
        title: "id",
        dataIndex: "id",
      },
      {
        title: "年度",
        dataIndex: "year",
      },
      {
        title: "参赛名称",
        dataIndex: "name",
      },
      {
        title: "参赛项目名称",
        dataIndex: "matchname",
      },
      {
        title: "项目负责人",
        dataIndex: "leader",
      },
      {
        title: "负责人id",
        dataIndex: "studentId",
      },
      {
        title: "获奖情况",
        dataIndex: "situation",
      },
      {
        title: "项目成员",
        dataIndex: "members",
      },
      {
        title: "指导老师",
        dataIndex: "teacher",
      },
      {
        title: "参赛级别",
        dataIndex: "level",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: "审核结果",
        dataIndex: "check",
      },
      {
        title: "附件文件",
        dataIndex: "file",
        width: 150,
      },
    ];
    //表头 及 对应的 数据字段名
    return {
      searchName:"",
      tableData: [],
      columns,
      isShow: false,
      checking: false,
      projectType: [],
      total: 0,
      current: 1,
    };
  },
  async created() {
    //获取页面初始数据
    this.init();
  },
  methods: {
    ToExcel: async function () {
      console.log(this.searchName.length);
      let message = 1;
      let description = "导出成功";
      if (this.searchName.length == 0) {
        message = 0;
        description = "请先搜索是否有合适的年份数据,再导出"
      } else if (this.searchName.length != 4 || !/^[1-9]+[0-9]*]*$/.test(this.searchName)) {
        message = 0;
        description = "请输入正确的年份"
      }

      this.$notification.open({
        key: message ? "success" : "updatable",
        message: message ? "导出成功" : "导出错误",
        description: description
      });
      if (message == 1) {
        let response = await Excel(this.searchName);
        downloadOfA(new Blob([response], {type: 'application/vnd.ms-excel'})
            , `${this.searchName}学生竞赛信息.xlsx`, `${this.searchName}学生竞赛信息.xlsx`);

      } else {
        console.log(123);
      }


    },
    //初始化数据
    async init(page = 1, size = 10) {
      const result = await getDataByPageAndSize(page, size);
      console.log(result);
      if (result.msg) {
        this.tableData = result.data.slice();
        this.current = page;
        this.total = result.count;
      }
    },

    // 删除 按钮回调
    async del(e) {
      console.log(e.id);
      const result = await deleteById(e.id);
      console.log(result);
      if (result.msg) {
        this.init();
      }
    },

    // 添加 或 编辑 弹窗的确定按钮
    async confirm(data) {
      this.closeDialog();
      const result = await this.confirmAjax(data);
      console.log(result);
      if (result.msg) {
        this.init();
      }
    },

    //发起添加 或 编辑 的请求
    async confirmAjax(data) {
      let edit = data.edit;
      let formData = data;

      this.$delete(data, "edit");
      this.$delete(data, "leader");

      data.status === "0" ? (data.status = 0) : (data.status = 1);
      console.log(data);
      if (typeof data.file != "string") {
        //生成数据
        formData = new FormData();
        Object.keys(data).forEach((item) => formData.append(item, data[item]));
      }

      if (edit) return editData(formData);
      return appendData(formData);
    },

    //发起 搜索 的请求
    async onSearch(name) {
      this.searchName = name;
      const result = await LikeSearch(name, 1, 10, 1);
      console.log(result);
      if (!result.msg) return;
      this.tableData = result.data.slice();
      this.total = result.count;
    },

    //审核按钮回调
    async checked(e) {
      this.closeDialog();
      await review(e);
      this.init();
    },
    //改变页码
    changePage(page) {
      this.init(page);
    },

    //显示全部 按钮回调
    showAll() {
      this.init();
    },

    //打开 添加 弹窗
    showModel() {
      this.isShow = true;
      this.row = {};
    },

    //打开 审核 弹窗
    handleCheck(e) {
      this.checking = true;
      console.log(e);
      this.row = { ...e, checking: true };
    },

    //打开 编辑 弹窗
    edit(e) {
      this.isShow = true;
      this.row = { ...e, edit: true };
      // console.log(this.row);
    },

    //弹唱的 取消 按钮
    cancel() {
      this.closeDialog();
    },

    //关闭弹窗
    closeDialog() {
      this.isShow = false;
      this.checking = false;
    },
  },
};
</script>

<style lang="less" scoped>
.Inventory {
  height: 100%;
  display: flex;
  flex-direction: column;
  .InventoryTable {
    flex: 1;
  }
}
</style>